<?php
/**
 * CRMLite Contact History Model
 *
 * @final
 * @package        Crmlite_Model
 * @version        $Id: ContactHistoryModel.php 65 2007-09-10 07:28:34Z etng2004 $
 * @copyright      2006-2007 ETE.CN
 * @author         Yi BO  <etng2004@gmail.com>
 * @license        PHP Version 3.0 {@link http://www.php.net/license/3_0.txt}
 */


/**
 * 联系历史组表
 */
class Crmlite_Db_Table_ContactHistory extends Crmlite_Db_Table {
    protected $_name = "contact_histories";
    protected $_primary = "id";
}

class ContactHistoryModel extends Crmlite_Model
{
    public function insert(&$record)
    {
        parent::insert($record);
        $record = $this->find($record['id']);
        $ClientModel =  new ClientModel();
        $cm_record = array(
        'last_contact_at' => $record['contact_at'],
        'last_contact_by' => $record['contact_by'],
        );
        $ClientModel->update($cm_record, 'id=' . $record['client_id']);
        return true;
    }
    public function detail_paginate($where = null, $order= null , $page = 1 ,$limit = self::DEFAULT_PERPAGE)
    {
        settype($where, 'array');
        settype($order, 'array');
        $offset = ($page - 1) * $limit;
        if(!$order)
        {
            $order = array();
            foreach ($this->primary as $pk)
            {
            	 $order []= $this->dbt->getTable() . ' . ' . $pk . ' DESC';
            }
        }

        $sql = "
		SELECT
clients.client_type_code,
clients.trade_type_id,
clients.company AS client_company,
clients.website AS client_website,
clients.alexa AS client_alexa,
clients.phone AS client_phone,
clients.fax AS client_fax,
clients.email AS client_email,
clients.address AS client_address,
clients.postcode AS client_postcode,
clients.stage AS client_stage,
clients.memo AS client_memo,
clients.contact_name AS client_contact_name,
clients.contact_email AS client_contact_email,
clients.contact_msn AS client_contact_msn,
clients.contact_qq AS client_contact_qq,
clients.contact_cellphone AS client_contact_cellphone,
clients.created_at AS client_created_at,
clients.created_by AS client_created_by,
clients.updated_at AS client_updated_at,
clients.updated_by AS client_updated_by,
contacts.is_primary AS contact_is_primary,
contacts.name AS contact_name,
contacts.email AS contact_email,
contacts.gender AS contact_gender,
contacts.marriage AS contact_marriage,
contacts.nation AS contact_nation,
contacts.address AS contact_address,
contacts.city AS contact_city,
contacts.msn AS contact_msn,
contacts.qq AS contact_qq,
contacts.phone AS contact_phone,
contacts.cellphone AS contact_cellphone,
contacts.memo AS contact_memo,
staffs.name AS staff_name,
staffs.id AS staff_id,
		  contact_histories.*
		FROM contact_histories
		LEFT JOIN contacts ON contact_histories.contact_id = contacts.id
		LEFT JOIN clients ON contact_histories.client_id = clients.id
		LEFT JOIN staffs ON clients.created_by = staffs.id
		";
        if($where)$sql .= ' WHERE ' . implode(' AND', $where);
        if($order)$sql .= ' ORDER BY ' . implode(', ', $order);
        if($limit)$sql .= " LIMIT {$offset}, {$limit}";
        return $this->db->fetchAll($sql);
    }
}